package jz.sats.config;

import jz.sats.advisor.ChatTypeHistoryAdvisor;
import jz.sats.advisor.ChatTypeTitleAdvisor;
import jz.sats.constant.SystemPrompt;
import jz.sats.enums.ChatType;
import jz.sats.service.ChatTypeHistoryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2025/7/9 0:02
 **/

@Configuration
public class AiAutoConfiguration {
//    @Bean
//    public ChatClient chatClient(@Qualifier("deepSeekChatModel") ChatModel deepSeekChatModel){
//        return ChatClient.builder(deepSeekChatModel).build();
//    }

    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(20)
                .build();
    }

    @Bean
    public ChatClient loveChatClient(@Qualifier("deepSeekChatModel") ChatModel deepSeekChatModel,
                                     ChatMemory chatMemory,
                                     ChatTypeHistoryService chatTypeHistoryService,
                                     @Qualifier("titleChatClient")ChatClient titleChatClient,
                                     Advisor retrievalAugmentationAdvisor) {

        return  ChatClient.builder(deepSeekChatModel)
                .defaultSystem(SystemPrompt.SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        ChatTypeHistoryAdvisor.builder(chatTypeHistoryService).type(ChatType.ASSISTANT.getCode())
                                .order(998).build(),
                        ChatTypeTitleAdvisor.builder(chatTypeHistoryService).type(ChatType.ASSISTANT.getCode())
                                .chatClient(titleChatClient).chatMemory(chatMemory).order(999).build(),
                        MessageChatMemoryAdvisor.builder(chatMemory).order(1000).build(),
                        retrievalAugmentationAdvisor
                )
                .build();
    }


    @Bean
    public ChatClient titleChatClient(@Qualifier("deepSeekChatModel") ChatModel deepSeekChatModel) {
        return ChatClient
                .builder(deepSeekChatModel)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                .build();
    }
}
